package org.gjw.security.component;

import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.gjw.bean.SysUser;
import org.gjw.service.SysUserService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author guojunwang
 * @date 2021-06-26 12:01
 */
@Data
@AllArgsConstructor
public class AuthUserDetails extends SysUser implements UserDetails {

    /**
     * 获取用户的所有权限
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return getPermissions().stream().filter(p -> StrUtil.isNotBlank(p.getPermission()))
                .map(p -> new SimpleGrantedAuthority(p.getPermission()))
                .collect(Collectors.toList());
    }

    @Override
    public boolean isAccountNonExpired() {
        return 0 == getDeleted();
    }

    @Override
    public boolean isAccountNonLocked() {
        return isAccountNonExpired();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return isAccountNonExpired();
    }

    @Override
    public boolean isEnabled() {
        return isAccountNonExpired();
    }
}
